package com.yanwq.sayhi.controller.article.activity;


import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.constant.RegexConstant;
import com.yanwq.sayhi.common.event.ArticleEvent;
import com.yanwq.sayhi.common.tool.SharedPrefTool;
import com.yanwq.sayhi.controller.user.UserMgr;
import com.yanwq.sayhi.dao.bean.Article;
import com.yanwq.sayhi.dao.bean.Club;
import com.yanwq.sayhi.widget.MToast;

import java.util.regex.Pattern;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class TextAddActivity extends BaseToolbarActivity {

    @BindView(R.id.article_summary)
    EditText mSummaryV;
    @BindView(R.id.article_text)
    EditText mTextV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_add);
    }

    @Override
    public void init() {
    }

    @Override
    public void initView() {
        mToolbar.setTitle("发文本");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_send, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        send();
        return true;
    }

    private void send(){
        if (!Pattern.matches(RegexConstant.summary, mSummaryV.getText().toString())) {
            MToast.showShort(TextAddActivity.this, "告诉大家你发的是什么~");
            return;
        }

        if (!Pattern.matches(RegexConstant.text, mTextV.getText().toString())) {
            MToast.showShort(TextAddActivity.this, "你没写东西~");
            return;
        }

        sendText();
    }

    private void sendText() {
        Article article = new Article();
        article.setSummary(mSummaryV.getText().toString());
        article.setText(mTextV.getText().toString());
        article.setLon(0);
        article.setLat(0);
        article.setType(0);
        article.setPermission(0);

        article.setUser(UserMgr.get());

        String path = "/article/add";

        if (SharedPrefTool.getConfig("mode", "").equals("club")) {
            Club club = new Club();
            club.setId(SharedPrefTool.getConfig("clubId", 0));
            article.setClub(club);
            path = "/article/add/club";
        }

        OkRestClient.getInstance().post(this, path, article,
                new OkJsonObjectResponseCallback() {
                    @Override
                    public void onStart() {
                        showProgressDialog("正在发布");
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        Article article = JSON.parseObject(jsonObject.toJSONString(),
                                Article.class);
                        MToast.showShort(TextAddActivity.this,
                                String.format("%s 发布成功", article.getSummary()));
                        delayFinish();

                        EventBus.getDefault().post(new ArticleEvent(
                                ArticleEvent.SEND_SUCCESS));
                    }

                    @Override
                    public void onStop() {
                        cancelProgressDialog();
                    }
                });
    }
}
